﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ShopSpace.Core;
using DotNetNuke.Common;
using DotNetNuke.Entities.Users;
using DotNetNuke.UI.Skins;

namespace ShopSpace.UI.DotNetNukeModules.Common
{
	public partial class EditPerson : EditModuleControlBase
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			try
			{
				this.SetupLinks();
				if (!this.Page.IsPostBack)
				{
					this.SetupForm();
					this.BindPerson();
				}
			}
			catch (Exception ex)
			{
				this.HandleException(ex);
			}
		}

		protected void lnkUpdate_Click(object sender, EventArgs e)
		{
			try
			{
				this.SavePerson();
				Response.Redirect(Globals.NavigateURL());
			}
			catch (Exception ex)
			{
				this.HandleException(ex);
			}
		}

		protected void ddlUsers_SelectedIndexChanged(object sender, EventArgs e)
		{
			try
			{
				UserInfo userInfo = UserController.GetUserByName(this.PortalId, this.ddlUsers.SelectedValue, true);
				this.txtDisplayName.Text = userInfo.DisplayName;
			}
			catch ( Exception ex )
			{
				this.HandleException(ex);
			}
		}

		private void SetupLinks()
		{
			this.hlCancel.NavigateUrl = Globals.NavigateURL();
		}

		private void SetupForm()
		{
			this.ddlUsers.DataSource = UserController.GetUsers(this.PortalId, true);
			this.ddlUsers.DataBind();
			foreach (Person existingPerson in this.PersonRepository.ListByPortal(this.PortalId))
			{
				if(this.editItemId.HasValue && existingPerson.Id == this.editItemId.Value)
				{
					continue;
				}
				foreach (ListItem item in this.ddlUsers.Items)
				{
					if (item.Value == existingPerson.Username)
					{
						this.ddlUsers.Items.Remove(item);
						break;
					}
				}
			}
			this.ddlUsers.Items.Insert(0, new ListItem("Select One", "0"));
		}

		private void BindPerson()
		{
			if (this.editItemId.HasValue)
			{
				Person person = this.PersonRepository.Get(this.editItemId.Value);
				this.ItemConcurrencyDate = person.ConcurrencyDate;
				this.ddlUsers.SelectedValue = person.Id.ToString();
			}
		}

		private void SavePerson()
		{
			Person person;
			if (this.editItemId.HasValue)
			{
				person = this.PersonRepository.Get(this.editItemId.Value);
				person.ConcurrencyDate = this.ItemConcurrencyDate;
				person.Username = this.ddlUsers.SelectedValue;
				person.DisplayName = this.txtDisplayName.Text.Trim();
			}
			else
			{
				person = new Person(this.PortalId, this.ddlUsers.SelectedValue, this.txtDisplayName.Text.Trim());
			}
			
			if (!this.editItemId.HasValue)
			{
				this.PersonRepository.Add(person, this.UserInfo.Username);
			}
			else
			{
				this.PersonRepository.Update(person, this.UserInfo.Username);
			}
			this.UserState.AddMessage(this.ModuleId, string.Format("{0} was {1}", person.DisplayName, this.editItemId.HasValue ? "updated" : "added"), DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess);
		}
	}
}